package com.ruoyi.web.controller.system;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Teacher;
import com.ruoyi.system.service.ITeacherService;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.utils.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;

/**
 * 教师 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-12-09
 */
@Controller
@RequestMapping("/system/teacher")
public class TeacherController extends BaseController
{
	private static final Logger log = LoggerFactory.getLogger(TeacherController.class);
    private String prefix = "system/teacher";
	
	@Autowired
	private ITeacherService teacherService;
	
	@RequiresPermissions("system:teacher:view")
	@GetMapping()
	public String teacher()
	{
	    return prefix + "/teacher";
	}
	
	/**
	 * 查询教师列表
	 */
	@RequiresPermissions("system:teacher:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Teacher teacher)
	{
		startPage();
        List<Teacher> list = teacherService.selectTeacherList(teacher);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出教师列表
	 */
	@Log(title = "教师管理", businessType = BusinessType.EXPORT)
	@RequiresPermissions("system:teacher:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Teacher teacher)
    {
    	List<Teacher> list = teacherService.selectTeacherList(teacher);
			ExcelUtil<Teacher> util = new ExcelUtil<Teacher>(Teacher.class);
			return util.exportExcel(list, "teacher");


    }
	/**
	 * 导入教师列表
	 */
	@Log(title = "教师管理", businessType = BusinessType.IMPORT)
	@RequiresPermissions("system:teacher:importExcel")
	@PostMapping("/importExcel")
	@ResponseBody
	public AjaxResult importExcel(@RequestParam("file") MultipartFile file)
	{
		if (file.isEmpty())
		{
			return AjaxResult.error("文件不能为空");
		}
		try {
			String fileName = file.getOriginalFilename();
			String suffix = fileName.substring(fileName.lastIndexOf("."));
			ExcelUtil<Teacher> util = new ExcelUtil<Teacher>(Teacher.class);
			List<Teacher> teacherList=util.importExcel(file.getInputStream());
			if (teacherList.size()>0){
				for (int i=1;i<teacherList.size();i++){
					Teacher teacher =new Teacher();
					teacher.setTeacherName(teacherList.get(i).getTeacherName());
					teacher.setEmail(teacherList.get(i).getEmail());
					teacherService.insertTeacher(teacher);
				}

			}
		} catch (FileNotFoundException e) {
			log.error("导入异常",e);
		} catch (Exception e) {
			log.error("保存数据",e);
		}
		return AjaxResult.success("导入成功");
	}
	/**
	 * 新增教师
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存教师
	 */
	@RequiresPermissions("system:teacher:add")
	@Log(title = "教师", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Teacher teacher)
	{
		// 获取当前的用户
		SysUser currentUser = ShiroUtils.getSysUser();
		if (currentUser != null)
		{
			teacher.setCreateBy(currentUser.getUserName());
			teacher.setCreateTime(new Date());
		}

		return toAjax(teacherService.insertTeacher(teacher));
	}

	/**
	 * 修改教师
	 */
	@GetMapping("/edit/{teacherId}")
	public String edit(@PathVariable("teacherId") Integer teacherId, ModelMap mmap)
	{
		Teacher teacher = teacherService.selectTeacherById(teacherId);
		mmap.put("teacher", teacher);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存教师
	 */
	@RequiresPermissions("system:teacher:edit")
	@Log(title = "教师", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Teacher teacher)
	{		
		return toAjax(teacherService.updateTeacher(teacher));
	}
	
	/**
	 * 删除教师
	 */
	@RequiresPermissions("system:teacher:remove")
	@Log(title = "教师", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(teacherService.deleteTeacherByIds(ids));
	}
	
}
